<template>
  <div class="form-input">
    <label class="field-label" :for="labelText" :style="labelStyle">
      {{ label }}
      <span class="required-field" v-if="!!required"></span>
    </label>
    <input
      class="field-input"
      :type="type"
      :name="name"
      :id="labelText"
      @keyup="handleChange"
      @change="handleChange"
      :value="value"
      :disabled="!!disabled"
    />
  </div>
</template>

<script>
export default {
  name: "Input",
  model: {
    props: "value",
    event: "change"
  },
  props: {
    required: Boolean,
    disabled: Boolean,
    value: {
      type: String,
      default: ""
    },
    name: {
      type: String,
      required: true
    },
    type: {
      type: String,
      default: "text"
    },
    label: {
      type: String,
      required: true
    },
    labelColor: {
      type: String,
      default: "#00C7CD"
    }
  },
  computed: {
    labelText() {
      const { name } = this;
      return `form-input__label-${name}`;
    },
    labelStyle() {
      const { labelColor: color } = this;
      return {
        color
      };
    }
  },
  methods: {
    handleChange(e) {
      const target = e.target || e.srcElement || e.toElement;
      const { value } = target;
      this.$emit("change", value);
    }
  }
};
</script>

<style lang="less">
@import "../assets/common.less";
.form-input {
  margin-top: 1.5em;

  .field-label {
    display: block;
    font-size: 12 / @fontsize * 1em;
    text-align: left;
    color: #00c7cd;
    position: relative;

    @required-point: 0.5em;
    .required-field {
      color: #ff5a1b;
      background-color: #ff5a1b;
      display: inline-block;
      width: @required-point;
      height: @required-point;
      border-radius: @required-point / 2;
      margin-left: 0.3em;
      transform: translateY(-0.15em);
    }
  }

  @input-height: 2.5em;
  .field-input {
    width: 100%;
    border: 0;
    height: @input-height;
    line-height: @input-height;
    background-color: transparent;
    outline: none;
    color: #ccc;
    font-size: 14 / @fontsize * 1em;
    border-bottom: 1 / @fontsize * 1em solid #ccc;
  }
}
</style>
